const fs = require('fs')
const { join } = require('path')

async function writeTemplate(name){
  const dirPath = join(__dirname, '..', 'src', 'component', name)
  
  if(!fs.existsSync(dirPath)){
    fs.mkdirSync(dirPath)
  }

  const template = `import React from 'react'
import clsx from 'clsx'
import { makeStyles } from '@material-ui/core/styles'

const useStyles = makeStyles({
  root: {

  },
})

interface IProps{

}

const ${name}: React.FC<IProps> = props => {
  const classes = useStyles()

  return <div
    className={clsx(
      classes.root,
    )}
  >
    
  </div>
}

export default ${name}
`
  const filePath = join(dirPath, `${name}.tsx`)
  fs.writeFileSync(filePath, template)
  console.log(`成功：创建文件 ${filePath}`)

  const template2 = `import ${name} from './${name}'

export default ${name}
`
  const filePath2 = join(dirPath, `index.ts`)
  fs.writeFileSync(filePath2, template2)
  console.log(`成功：创建文件 ${filePath2}`)
}

async function start(){
  const name = process.argv[2]
  if(!name){
    console.log(`错误：缺少名字`)
    return
  }
  await writeTemplate(name)
  
}

start()